cd "$pathdata/Survey_Beliefs"

use Survey_cleaned_final, clear





**** Histogram of Cognitive Uncertainty

graph twoway (histogram cognitive_uncertainty, frac gap(15) lcolor(black%20)   fcolor(black%20) width(0.049)),  ///
	title("Stock market expectations", color(black)) ///
	ytitle("Fraction",) xtitle("Cognitive uncertainty") ///
	graphregion(fcolor(white) lcolor(white)) ///
	ylabel(0(0.04)0.16,glcolor(gs15) ang(h)) xmtick(#20) ///
	yline(0.16,lstyle(grid)) ///
	xsize(8) ysize(8) ///
	ysc(range(0 0.17) lcolor(none)) xsc(lcolor(none)) 


	
	

	
* Main figure

preserve

egen cu_cutoff = pctile(cognitive_uncertainty), p(50)
gen uncertain=(cognitive_uncertainty>cu_cutoff)

egen bucket=group(uncertain true_percentile)
gen se_belief=.

forvalues i=1/18{
	cap qui qreg2 belief if bucket==`i', cluster(id)
	cap qui replace se_belief=_se[_cons] if bucket==`i'
}


collapse (p50) belief cognitive_uncertainty se_belief (count) no=belief, by(uncertain true_percentile)

keep if no>=30

gen upper=belief+se_belief
gen lower=belief-se_belief

tw (scatter belief true_percentile if uncertain==0, msize(medsmall) ms(o) mcolor(midblue%90)) ///
 (scatter belief true_percentile if uncertain==1, msize(medium) ms(X) mcolor(red%80))  ///
 	(rcap upper lower true_percentile if uncertain==0, msiz(large) lcolor(midblue%90))	///
	(rcap upper lower true_percentile  if uncertain==1, msiz(large) lcolor(red%80))	///
	(function y=x , range(0 100) lpattern(dash) lcolor(black) lwidth(thin)), ///
	title("Stock market expectations", color(black)) ///
	ytitle("Belief") ///
    xtitle("Historical probability") xscale( lcolor(none)) ysc(lcolor(none)) ///
	yline(0, lcolor(gs10) lwidth(thin)) ///
	graphregion(color(white)) ///
	ylabel(, tlc(none) angle(0) glcolor(gs15) glwidth(thin)) ///
	legend(order(1 2 3 5) label(1 "Low cognitive uncertainty") label(2 "High cognitive uncertainty") label(3 "{c 177}1 std. error of median") label(5 "Historical frequencies") r(2)) 


restore


*** Figure Distance to optimal decision

binscatter abs_dist cognitive_uncertainty, ///
		xtitle("Cognitive uncertainty") ytitle("Distance b/w forecast and historical prob.") xscale(lcolor(none)) yscale(lcolor(none)) ///
		title("Stock market expectations", color(black)) ///
		yline(10, lcolor(gs10) lwidth(thin)) ///
		graphregion(color(white)) ///
		ms(+) mcolor(black) lcolor(red%80) ///
		xsize(8) ysize(8) ///
		ylabel(, tlc(none) angle(0) glcolor(gs15) glwidth(thin))
		

spearman abs_dist cognitive_uncertainty


**** Quartiles

xtile q = cognitive_uncertainty, nquantile(4)

forvalues i=1/4{
	reg belief true_percentile if q == `i', cl(id)
	estimates store v`i'
}

coefplot (v1, color(black) ciopts(color(black))) (v2, color(black) ciopts(color(black))) (v3, color(black) ciopts(color(black))) (v4, color(black) ciopts(color(black))), drop(_cons) yline(0, lcolor(grey) lpattern(dash)) vertical ysc(lcolor(none)) xscale( lcolor(none))  ylabel(0 (0.2) 0.8, tlc(none) angle(0) glcolor(gs15) glwidth(thin)) ysc(r(-0.005,0.04) lcolor(none)) xlabel(0.7 `""CU Q1" "(ave. = 13.6)""' 0.9 `""CU Q2" "(ave. = 41.7)""' 1.1 `""CU Q3" "(ave. = 70.5)""' 1.3 `""CU Q4" "(ave. = 92.9)""') yline(0, lcolor(gs10) lwidth(thin)) grid(glcolor(gs15))  graphregion(color(white) fcolor(white)) ytitle("Regression coefficient and 95% CI") title("Effect of historical probability on stock market beliefs", color(black)) legend(off)



